/*
 * fe8event_dump.c
 *
 *  Created on: 2015年10月1日
 *      Author: yami
 */

/**
 * @file	fe8event_dump.c
 * @author	yami
 * @date	2015年10月1日
 * @brief
 * 
 */

#include <stdint.h>

#include <stdlib.h>
#include <stdio.h>

typedef uint32_t	u32;
typedef uint16_t	u16;
typedef uint8_t		u8;

typedef int8_t		s8;
typedef int16_t		s16;
typedef int32_t		s32;


FILE*		out = 0;

const char Hack[]	=	"EVENT_";
char *eventPrefix	=	"";
const char suffix[]	=	"";
char *eventSuffix	=	"";
const char *comm	=	"未知";
u32		pointer		=	0;

typedef struct{
	FILE*		file;
	u8*			mem;
	s32			size;
}ROM;

int
addr_is_agb(u32 addr){
	if(addr>=0x8000000&&addr<=0xa000000){
		return 1;
	}
	return 0;
}


ROM*
rom_new(const char*fname)
{
	ROM		*rom;
	FILE	*f;
	f	=	fopen(fname,"rb");
	if(!f)
		return 0;
	rom			=	calloc(1,sizeof(*rom));
	rom->file	=	f;
	if(!rom->file)
		return 0;
	return rom_init(rom);
}

ROM*
rom_init(ROM*rom){
	fseek(rom->file,0,SEEK_END);
	rom->size	=	ftell(rom->file);
	fseek(rom->file,0,SEEK_SET);
	rom->mem	=	calloc(1,rom->size);
	if(!rom->mem)
	{
		rom_free(rom);
		return 0;
	}
	fread(rom->mem,1,rom->size,rom->file);
	return rom;
}

void
rom_free(ROM*rom){
	if(rom)
	{
		if(rom->mem)
			free(rom->mem);
		if(rom->file)
			fclose(rom->file);
		free(rom);
	}
}

void*
rom_data(ROM*rom,u32 addr){
	if(!addr_is_agb(addr))
		return 0;
	addr	-=	0x08000000;
	if(addr>rom->size)
		return 0;
	return rom->mem + addr;
}


typedef int (*eventfunc)(u16*);


#define _00 _end
int
_end(u16*e){
	return -1;
}

#define _01  EVENT_ENDA
int
EVENT_ENDA(u16*e){
	//ENDA
	if(out){
		fprintf(out,"%s%s%s  \t\t//%s\n",eventPrefix,"ENDA",eventSuffix,"返回");
	}
	return 1;
}



#define DEF_EFUNC(id)  int _##id (u16*e){\
	int l;\
	l	=	((e[0]>>4)&0xf);\
	if(out){\
		if(l==2){\
			fprintf(out,"%s%s%s  ,\t\t//%s\n",eventPrefix,__FUNCTION__,eventSuffix,"未知");\
		}\
		else{\
			fprintf(out,"%s%s%s(",eventPrefix,__FUNCTION__,eventSuffix);\
			int	i;\
			for(i=2;i<l;){\
				fprintf("0x%04X",e[i]);\
				if(++i!=l){\
					fprintf(",");\
				}\
			}\
			fprintf(out,"),  \t\t//%s\n",eventSuffix,__FUNCTION__);\
		}\
	}\
	return l;\
	}


#define DEF_EFUNC_A16_	\
		int l;\
		l	=	((e[0]>>4)&0xf);\
		if(out){\
			if(l==2){\
				fprintf(out,"%s%s%s  ,\t\t//%s\n",eventPrefix,__FUNCTION__,eventSuffix,comm);\
			}\
			else{\
				fprintf(out,"%s%s%s(",eventPrefix,__FUNCTION__,eventSuffix);\
				int	i;\
				for(i=2;i<l;){\
					fprintf("0x%04X",e[i]);\
					if(++i!=l){\
						fprintf(",");\
					}\
				}\
				fprintf(out,"),  \t\t//%s\n",eventSuffix,__FUNCTION__);\
			}\
		}\
		return l;


#define DEF_EFUNC_POINT_\
		int l;\
		l	=	((e[0]>>4)&0xf);\
		if(out){\
			if(l==2){\
				fprintf(out,"%s%s%s  ,\t\t//%s\n",eventPrefix,__FUNCTION__,eventSuffix,comm);\
			}\
			else{\
				fprintf(out,"%s%s%s(0x%08X",eventPrefix,__FUNCTION__,eventSuffix,pointer);\
				fprintf(out,"),  \t\t//%s\n",eventSuffix,__FUNCTION__);\
			}\
		}\
		return l;


int
EVENT_SETCONDITION(u16*e){
	const char*	comm="分支";
	char*	eventSuffix	=	"";
	if(e[0]&1){
		eventSuffix	=	"2";
	}
	DEF_EFUNC_A16_;
}

int
EVENT_ASMC(u16*e){
	const char*	comm="未清楚";
	char*	eventSuffix	=	"";
	u32		pointer	=	((u32*)e+2)[0];
	if(e[0]&1){
		eventSuffix	=	"2";
	}
	DEF_EFUNC_POINT_;
}


#define DEF_OFFSET2_A16 \
		char	*eventSuffix;\
		char	buf[0x20];\
		eventSuffix		=	buf;\
		sprintf(buf,"(0x%04X)",e[1]);

int
EVENT_STAL(u16*e){
	DEF_OFFSET2_A16
	DEF_EFUNC_A16_;
}

int
SWITCHING_MUSIC(u16*e){
	DEF_OFFSET2_A16;
	DEF_EFUNC_A16_;
}

int
EVENT_MUSICSOMETHING(u16*e){
	DEF_EFUNC_A16_;
	DEF_EFUNC_A16_;
}



DEF_EFUNC(00);
DEF_EFUNC(01);
DEF_EFUNC(02);
DEF_EFUNC(03);
DEF_EFUNC(04);
DEF_EFUNC(05);
DEF_EFUNC(06);
DEF_EFUNC(07);
DEF_EFUNC(08);
DEF_EFUNC(09);
DEF_EFUNC(0A);
DEF_EFUNC(0B);
DEF_EFUNC(0C);
DEF_EFUNC(0D);
DEF_EFUNC(0E);
DEF_EFUNC(0F);
DEF_EFUNC(10);
DEF_EFUNC(11);
DEF_EFUNC(12);
DEF_EFUNC(13);
DEF_EFUNC(14);
DEF_EFUNC(15);
DEF_EFUNC(16);
DEF_EFUNC(17);
DEF_EFUNC(18);
DEF_EFUNC(19);
DEF_EFUNC(1A);
DEF_EFUNC(1B);
DEF_EFUNC(1C);
DEF_EFUNC(1D);
DEF_EFUNC(1E);
DEF_EFUNC(1F);
DEF_EFUNC(20);
DEF_EFUNC(21);
DEF_EFUNC(22);
DEF_EFUNC(23);
DEF_EFUNC(24);
DEF_EFUNC(25);
DEF_EFUNC(26);
DEF_EFUNC(27);
DEF_EFUNC(28);
DEF_EFUNC(29);
DEF_EFUNC(2A);
DEF_EFUNC(2B);
DEF_EFUNC(2C);
DEF_EFUNC(2D);
DEF_EFUNC(2E);
DEF_EFUNC(2F);
DEF_EFUNC(30);
DEF_EFUNC(31);
DEF_EFUNC(32);
DEF_EFUNC(33);
DEF_EFUNC(34);
DEF_EFUNC(35);
DEF_EFUNC(36);
DEF_EFUNC(37);
DEF_EFUNC(38);
DEF_EFUNC(39);
DEF_EFUNC(3A);
DEF_EFUNC(3B);
DEF_EFUNC(3C);
DEF_EFUNC(3D);
DEF_EFUNC(3E);
DEF_EFUNC(3F);
DEF_EFUNC(40);
DEF_EFUNC(41);
DEF_EFUNC(42);
DEF_EFUNC(43);
DEF_EFUNC(44);
DEF_EFUNC(45);
DEF_EFUNC(46);
DEF_EFUNC(47);
DEF_EFUNC(48);
DEF_EFUNC(49);
DEF_EFUNC(4A);
DEF_EFUNC(4B);
DEF_EFUNC(4C);
DEF_EFUNC(4D);
DEF_EFUNC(4E);
DEF_EFUNC(4F);
DEF_EFUNC(50);
DEF_EFUNC(51);
DEF_EFUNC(52);
DEF_EFUNC(53);
DEF_EFUNC(54);
DEF_EFUNC(55);
DEF_EFUNC(56);
DEF_EFUNC(57);
DEF_EFUNC(58);
DEF_EFUNC(59);
DEF_EFUNC(5A);
DEF_EFUNC(5B);
DEF_EFUNC(5C);
DEF_EFUNC(5D);
DEF_EFUNC(5E);
DEF_EFUNC(5F);
DEF_EFUNC(60);
DEF_EFUNC(61);
DEF_EFUNC(62);
DEF_EFUNC(63);
DEF_EFUNC(64);
DEF_EFUNC(65);
DEF_EFUNC(66);
DEF_EFUNC(67);
DEF_EFUNC(68);
DEF_EFUNC(69);
DEF_EFUNC(6A);
DEF_EFUNC(6B);
DEF_EFUNC(6C);
DEF_EFUNC(6D);
DEF_EFUNC(6E);
DEF_EFUNC(6F);
DEF_EFUNC(70);
DEF_EFUNC(71);
DEF_EFUNC(72);
DEF_EFUNC(73);
DEF_EFUNC(74);
DEF_EFUNC(75);
DEF_EFUNC(76);
DEF_EFUNC(77);
DEF_EFUNC(78);
DEF_EFUNC(79);
DEF_EFUNC(7A);
DEF_EFUNC(7B);
DEF_EFUNC(7C);
DEF_EFUNC(7D);
DEF_EFUNC(7E);
DEF_EFUNC(7F);




eventfunc
eventlist[] = {
		_end,
		_01,
		_02,
		_03,
		_04,
		_05,
		_06,
		_07,
		_08,
		_09,
		_0A,
		_0B,
		_0C,
		_0D,
		_0E,
		_0F,
		_10,
		_11,
		_12,
		_13,
		_14,
		_15,
		_16,
		_17,
		_18,
		_19,
		_1A,
		_1B,
		_1C,
		_1D,
		_1E,
		_1F,
		_20,
		_21,
		_22,
		_23,
		_24,
		_25,
		_26,
		_27,
		_28,
		_29,
		_2A,
		_2B,
		_2C,
		_2D,
		_2E,
		_2F,
		_30,
		_31,
		_32,
		_33,
		_34,
		_35,
		_36,
		_37,
		_38,
		_39,
		_3A,
		_3B,
		_3C,
		_3D,
		_3E,
		_3F,
		_40,
		_41,
		_42,
		_43,
		_44,
		_45,
		_46,
		_47,
		_48,
		_49,
		_4A,
		_4B,
		_4C,
		_4D,
		_4E,
		_4F,
		_50,
		_51,
		_52,
		_53,
		_54,
		_55,
		_56,
		_57,
		_58,
		_59,
		_5A,
		_5B,
		_5C,
		_5D,
		_5E,
		_5F,
		_60,
		_61,
		_62,
		_63,
		_64,
		_65,
		_66,
		_67,
		_68,
		_69,
		_6A,
		_6B,
		_6C,
		_6D,
		_6E,
		_6F,
		_70,
		_71,
		_72,
		_73,
		_74,
		_75,
		_76,
		_77,
		_78,
		_79,
		_7A,
		_7B,
		_7C,
		_7D,
		_7E,
		_7F,
};



int
event_outC(ROM*rom,u32 addr){
	u16		*eventptr;
	eventptr	=	rom_data(rom,addr);
	return 0;
}


int
main(int argc,char**argv)
{
	return 0;
}

